<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

/**
 * 后台参数字典控制器
 *
 * @author leolei <346991581@qq.com>
 */
class Dict extends BasicAdmin
{

    /**
     * 当前默认数据模型
     * @var string
     */
    public $table = 'SystemDict';

    /**
     * 当前页面标题
     * @var string
     */
    public $title = '参数字典';

    /**
     * 字典列表
     *
     * @return void
     * @author leolei <346991581@qq.com>
     */
    public function index()
    {
        $this->assign('actions', Db::name($this->table)->group('type')->column('type'));
        $db = Db::name($this->table)->order('type asc,value asc,id desc');
        $get = $this->request->get();
        foreach (['name', 'type'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "%{$get[$key]}%");
            }
        }
        return parent::_list($db);
    }

    /**
     * 添加字典
     */
    public function add()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 编辑字典
     */
    public function edit()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 删除字典
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("字典删除成功！", '');
        }
        $this->error("字典删除失败，请稍候再试！");
    }

    /**
     * 字典禁用
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("字典禁用成功！", '');
        }
        $this->error("字典禁用失败，请稍候再试！");
    }

    /**
     * 字典启用
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("字典启用成功！", '');
        }
        $this->error("字典启用失败，请稍候再试！");
    }
}
